/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.company.regis.services.impl;

import com.company.regis.bean.UserCriteria;
import com.company.regis.db.entity.ViewUserDetail;
import com.company.regis.services.UserServices;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author totoland
 */
public class LazyViewUserImpl extends LazyDataModel<ViewUserDetail> {

    private static final long serialVersionUID = -7332140435235556716L;
    private static Logger logger = LoggerFactory.getLogger(LazyViewUserImpl.class);
    
    private List<ViewUserDetail> datasource;
    private int pageSize = 0;
    private int rowIndex = 0;
    private int rowCount = 0;
    private UserCriteria userCriteria;
    private UserServices userServices;

    @Override
    public List<ViewUserDetail> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {

        userCriteria.setStartRow(first);
        userCriteria.setMaxRow(pageSize);
        
        datasource = userServices.findByCriteria(userCriteria);

        return datasource;

    }

    @Override
    public boolean isRowAvailable() {
        if (datasource == null) {
            return false;
        }
        int index = rowIndex % pageSize;
        return index >= 0 && index < datasource.size();
    }

    @Override
    public Object getRowKey(ViewUserDetail user) {
        return user.getUserId();
    }

    @Override
    public ViewUserDetail getRowData() {
        if (datasource == null) {
            return null;
        }
        int index = rowIndex % pageSize;
        if (index > datasource.size()) {
            return null;
        }
        return datasource.get(index);
    }

    @Override
    public ViewUserDetail getRowData(String rowKey) {
        if (datasource == null) {
            return null;
        }
        for (ViewUserDetail user : datasource) {
            if ((user.getUserId()+ "").equals(rowKey)) {
                return user;
            }
        }
        return null;
    }

    @Override
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public int getPageSize() {
        return pageSize;
    }

    @Override
    public int getRowIndex() {
        return this.rowIndex;
    }

    @Override
    public void setRowIndex(int rowIndex) {
        this.rowIndex = rowIndex;
    }

    @Override
    public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
    }

    @Override
    public int getRowCount() {
        return this.rowCount;
    }

    @Override
    public void setWrappedData(Object list) {
        this.datasource = (List<ViewUserDetail>) list;
    }

    @Override
    public Object getWrappedData() {
        return datasource;
    }

    /**
     * @return the userCriteria
     */
    public UserCriteria getUserCriteria() {
        return userCriteria;
    }

    /**
     * @param userCriteria the userCriteria to set
     */
    public void setUserCriteria(UserCriteria userCriteria) {
        this.userCriteria = userCriteria;
    }

    /**
     * @return the userServices
     */
    public UserServices getUserServices() {
        return userServices;
    }

    /**
     * @param userServices the userServices to set
     */
    public void setUserServices(UserServices userServices) {
        this.userServices = userServices;
    }
    
}
